<?php

class UserSession {
	var $CI;

	function __construct() {
		$this->CI = & get_instance();
		$this->CI->load->model('model_usersession');
	}

	/**
	 * Verificare daca userul curent este logat
	 * @return boolean
	 */
	function userLoggedIn() {
		$aUser = $this->CI->session->userdata('userdetails');
		if ($aUser === FALSE) {
			return FALSE;
		}
		if (! isset($aUser['user_id']) || (empty($aUser['user_id'])))
			return FALSE;
		return TRUE;
	}

	/**
	 * Returnare informatii user logat
	 * @return array
	 */
	function currentUser() {
		return $this->CI->session->userdata('userdetails');
	}

	/**
	 * Creare sesiune pentru user
	 * @return void
	 * @param array $aUser array ce contine userdata (tbl. "user")
	 */
	function loginUser($aUser) {

		$aToSession = $aUser;
		//stergere sesiuni ale aceluiasi utilizator
		if ($this->accountConnected($aUser['user_id'])) {
			$this->CI->model_usersession->deleteSession($aUser['user_id']);
		}

		$this->CI->session->set_userdata('userdetails', $aToSession);
		$sSessionId = $this->CI->session->userdata('session_id');

		//creare sesiune login
		$this->CI->model_usersession->insertSession($sSessionId, $aUser['user_id']);
	}

	/**
	 * Functie de delogare user
	 * @return void
	 */
	function logoutUser() {
		$this->CI->session->sess_destroy();
	}

	/**
	 * Verificare daca un user este deja logat
	 * @return bool
	 * @param int $iUserId
	 */
	function accountConnected($iUserId) {
		$aSessionIdList = $this->CI->model_usersession->getSession($iUserId);
		return ((count($aSessionIdList) === 0) ? FALSE : TRUE);
	}
}

/* End of file UserSession.php */
/* Location: ./system/application/libraries/UserSession.php */
?>